package com.sky.blog.util;

import org.jetbrains.annotations.NotNull;
import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.core.env.Environment;
import org.springframework.stereotype.Component;

/**
 * @author sky  2022/3/26
 * @version 1.0
 */

@Component
public class ApplicationContextUtils implements ApplicationContextAware {

    private static ApplicationContext app;

    private static Environment env;

    @Override
    public void setApplicationContext(@NotNull ApplicationContext applicationContext) throws BeansException {
        app = applicationContext;
        env = app.getEnvironment();

    }

    public static Object getBeanByName(String beanName) {
        return app.getBean(beanName);
    }

    public static <T> T getBeanByType(Class<? extends T> beanType) {
        return app.getBean(beanType);
    }

    public static String getApplicationPath() {
        return env.getProperty("blog.app.path");
    }

    public static String getAppProperty(@NotNull String key) {
        return env.getProperty(key);
    }
}
